package com.example.administrator.maizuofilm.recommend.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.maizuofilm.BaseFragment;
import com.example.administrator.maizuofilm.R;
import com.example.administrator.maizuofilm.bean.Recommend;
import com.example.administrator.maizuofilm.moviedetails.MovieDetailsActivity;
import com.example.administrator.maizuofilm.recommend.adapter.RecommendAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by Administrator on 2016/7/12.
 */
public class RecommendFragment extends BaseFragment implements RecommendContract.RecommendView {
    @InjectView(R.id.recycler_recommend_movie)
    RecyclerView recyclerRecommendMovie;
    private RecommendModel recommendModel;
    private RecommendPresenter recommendPresenter;
    private RecommendAdapter adapter;
    private List<Recommend.DataBean> list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_recommend, null);
        ButterKnife.inject(this, view);
        recommendPresenter.init();
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerRecommendMovie.setLayoutManager(linearLayoutManager);
        list = new ArrayList<>();
        adapter = new RecommendAdapter(list,getActivity());
        recyclerRecommendMovie.setAdapter(adapter);
        adapter.setListener(new RecommendAdapter.RecommendListener() {
            @Override
            public void onImageViewListener(View v, int position) {

                Intent intent=new Intent(getActivity(), MovieDetailsActivity.class);


                intent.putExtra("filmID",list.get(position).getFilmId());
                startActivity(intent);
            }

            @Override
            public void onDetailListener(View v, int position) {

                Intent intent=new Intent(getActivity(), MovieDetailsActivity.class);


                intent.putExtra("filmID",list.get(position).getFilmId());
                startActivity(intent);
            }

            @Override
            public void onLikeListener(View v, int position) {

            }
        });
        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        recommendModel = new RecommendModel();
        recommendPresenter = new RecommendPresenter(recommendModel, this);
    }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onInitSuccess(Recommend recommend) {

        list.addAll(recommend.getData());
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onInitFail(String failMsg) {


    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

}
